home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / complexround.pro < prev    next >
Text File  |  1997-07-08  |  2KB  |  73 lines

  1. ;$Id: complexround.pro,v 1.5 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1994-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       COMPLEXROUND
  8. ;
  9. ; PURPOSE:
  10. ;       This function rounds a complex scalar or array.
  11. ;
  12. ; CATEGORY:
  13. ;       Numerical Analysis.
  14. ;
  15. ; CALLING SEQUENCE:
  16. ;       Result = Complexround(z)
  17. ;
  18. ; INPUTS:
  19. ;       Z: A complex scalar or array.
  20. ;
  21. ; RESTRICTIONS:
  22. ;       The input argument must be complex.
  23. ;
  24. ; PROCEDURE:
  25. ;       This function rounds the real and imaginary components of the 
  26. ;       complex input argument.
  27. ;
  28. ; EXAMPLE:
  29. ;       ;Define a complex array.
  30. ;       z = [[complex(1.245, 3.880), complex( 1.245, -3.880)], $
  31. ;              [complex(1.499, 5.501), complex(-1.355, -2.115)]]
  32. ;       ;Round it.
  33. ;         result = complexround(z) 
  34. ;
  35. ; MODIFICATION HISTORY:
  36. ;       Written by:  GGS, RSI, September 1992
  37. ;       Modified:    GGS, RSI, September 1994
  38. ;                    Added support for double-precision complex inputs.
  39. ;                    Uses IDL's intrinsic ROUND function.
  40. ;-
  41.  
  42. function complexround, z
  43.  
  44.   ;dimension = size(input)  ;Size of input array.
  45.   ;output = complexarr(dimension(1), dimension(2))
  46.   ;real = float(input) ;Separate into real and imaginary. 
  47.   ;imag = imaginary(input)
  48.  
  49.   ;z1 = real + 0.5 ;Round real components.
  50.   ;neg1 = where(real lt 0, count1)
  51.   ;if count1 ne 0 then z1(neg1) = z1(neg1) - 1
  52.   ;z1 = fix(z1)
  53.  
  54.   ;z2 = imag + 0.5 ;Round imaginary components.
  55.   ;neg2 = where(imag lt 0, count2)
  56.   ;if count2 ne 0 then z2(neg2) = z2(neg2) - 1
  57.   ;z2 = fix(z2)
  58.  
  59.   ;output = complex(z1,z2)
  60.   ;return, complex(z1,z2)
  61.  
  62.   on_error, 2
  63.  
  64.   sz = size(z)
  65.  
  66.   if sz[sz[0]+1] eq 6 then begin 
  67.     return, complex(round(float(z)), round(imaginary(z))) 
  68.   endif else if sz[sz[0]+1] eq 9 then $
  69.     return, dcomplex(round(float(z)), round(imaginary(z))) $
  70.   else message, 'Input must be of complex type.'
  71.  
  72. end
  73.